//
//  YSFormOptionsCell.m
//  Form
//
//  Created by 方鹏俊 on 2017/11/9.
//  Copyright © 2017年 亚厦装饰股份有限公司. All rights reserved.
//

#import "YSFormOptionsCell.h"

@interface YSFormOptionsCell ()

@property (nonatomic, strong) QMUIAlertController *alertController;

@end

@implementation YSFormOptionsCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initUI];
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return self;
}

- (void)initUI {
    [self addTitleLabel];
    [self addDetailLabel];
}

- (void)setCellModel:(YSFormRowModel *)cellModel {
    [super setCellModel:cellModel];
    self.detailTextLabel.text = self.cellModel.detailTitle;
     _alertController = [QMUIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:QMUIAlertControllerStyleActionSheet];
    QMUIAlertAction *action0 = [QMUIAlertAction actionWithTitle:@"取消" style:QMUIAlertActionStyleDestructive handler:^(QMUIAlertAction *action) {
    }];
    [self.alertController addAction:action0];
   
    for (int i = 0; i < self.cellModel.optionsDataArray.count; i ++) {
       
        NSDictionary *dic = self.cellModel.optionsDataArray[i];
         YSWeak;
        QMUIAlertAction *action = [QMUIAlertAction actionWithTitle:[dic allValues][0] style:QMUIAlertActionStyleDefault handler:^(QMUIAlertAction *action) {
            weakSelf.cellModel.detailTitle = [dic allValues][0];
            weakSelf.detailLabel.text = [dic allValues][0];
            [weakSelf.sendValueSubject sendNext:weakSelf.cellModel.optionsReturnType == YSFormOptionsReturnKey ? [dic allKeys][0] : [dic allValues][0]];
            [weakSelf.sendOptionsSubject sendNext:@{[dic allKeys][0] : [dic allValues][0]}];
            YSFormCellModel *formCellModel = [[YSFormCellModel alloc] init];
            formCellModel.value = weakSelf.cellModel.optionsReturnType == YSFormOptionsReturnKey ? [dic allKeys][0] : [dic allValues][0];
            formCellModel.indexPath = weakSelf.cellModel.indexPath;
            [weakSelf.sendFormCellModelSubject sendNext:formCellModel];
        }];
        [self.alertController addAction:action];
    }
}
#pragma mark - 用户选中cell时被调用
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    if ([self.cellModel.title isEqualToString:@"职务级别"]) {
        return;
    }
    if (self.selected == selected) return;
    [super setSelected:selected animated:animated];
    if (selected) {
        if (!self.cellModel.disable) {
            [self.alertController showWithAnimated:YES];
            [self setCellModel:self.cellModel];
        }
        self.selected = NO;
    }
}
- (void)dealloc {
    NSLog(@"----");
}
@end
